package com.project.dao.impl;

import com.project.bean.UserBean;
import com.project.dao.IUserDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl extends BaseDao implements IUserDao {
    @Override
    public UserBean login(String userName, String password) {
            List list =null;
            this.setConnection();
        try {
            this.ps=this.con.prepareStatement("select * from t_user where u_name =? and u_password =?");
            this.ps.setObject(1,userName);
            this.ps.setObject(2,password);
            list=(this.getObject(this.ps.executeQuery()));

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
        if (list.size()==0){
            return null;
        }else {
            return (UserBean)list.get(0);
        }

    }

    @Override
    public void add(UserBean user) {
        this.setConnection();
        try {
            this.ps = this.con.prepareStatement("insert into t_user(u_name,u_password,u_birthday)"+" values(?,?,?)");
            this.ps.setObject(1,user.getName());
            this.ps.setObject(2,user.getPassword());
            this.ps.setObject(3,user.getBirthday());
            this.ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }

    }

    @Override
    public void del(int id) {
        this.setConnection();
        try {
            this.ps=this.con.prepareStatement("delete from t_user where pk_userId =?");
            this.ps.setObject(1,id);
            this.ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public void update(int id, String password) {
        this.setConnection();
        try {
            this.ps=this.con.prepareStatement("update t_user  set u_password=? where pk_userId =?");
            this.ps.setObject(1,password);
            this.ps.setObject(2,id);
            this.ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public List<UserBean> findAll() {
        List<UserBean> list =null;
        this.setConnection();
        try {
            this.ps=this.con.prepareStatement("select * from t_user");
            list=this.getObject(this.ps.executeQuery());

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
        return list;
    }

    @Override
    public UserBean findById(int id) {
        List list = null;
        this.setConnection();
        try {
            this.ps=this.con.prepareStatement("select * from t_user where pk_userId=?");
            this.ps.setObject(1,id);
            list=this.getObject(this.ps.executeQuery());

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
        return (UserBean)list.get(0);
    }
}
